package com.yjhb.android.sbz.fragment.taskmanage;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import com.yjhb.android.sbz.activity.ManagerCenterActivity;
import com.yjhb.android.sbz.adapter.taskmanage.TaskManagerFailAdapter;
import com.yjhb.android.sbz.dialog.InputDialog;
import com.yjhb.android.sbz.dialog.TipsDialog;
import com.yjhb.android.sbz.entity.AddTask;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.ManagerTask;
import com.yjhb.android.sbz.enums.TaskState;
import com.yjhb.android.sbz.tool.CommonDialogCollect;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;

/**
 * 创建者 zx
 * 创建时间 2020/11/20 17:33
 */
public class FailFragment extends BaseTaskManageFragment<TaskManagerFailAdapter> {
    private final String TAG = "FailFragment";


    @Override
    public CharSequence getTitle() {
        return "审核失败";
    }

    @Override
    protected TaskManagerFailAdapter getAdapter() {
        return new TaskManagerFailAdapter(getContext());
    }


    @Override
    protected CharSequence getTips() {
        return "若项目单价过低被拒，可加价后，点击“重新编辑”申请发布~";
    }

    @Override
    protected Integer getState() {
        return TaskState.TASK_STATE_FAIL.getState();
    }

    @Override
    public void onItemClick(int index, Object o) {
        super.onItemClick(index, o);
        Intent intent = new Intent(getContext(), ManagerCenterActivity.class);
        intent.putExtra("state", getState());
        intent.putExtra("taskId", dataSet.get(index).getTask().getId());
        startActivity(intent);
    }

    @Override
    public void onSuccess(String tips, String taskId) {
        super.onSuccess(tips, taskId);
        if (tips.equals("退款成功！")) {
            EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
        }
    }

    @Override
    public void onViewClick(View view, String msg, int index, Object o) {
        super.onViewClick(view, msg, index, o);
        ManagerTask managerTask = (ManagerTask) o;
        if (msg != null && msg.equals("reedit")) {
            loadingDialog.show();
            loadingDialog.setTipsText("正在拉取数据..");
            presenter.getDetails(dataSet.get(index));
        } else if (msg != null && msg.equals("apply_refund")) {
            TipsDialog tipsDialog = new TipsDialog(getContext());
            tipsDialog.setTitle("提示");
            tipsDialog.setContent("确认申请退款？");
            tipsDialog.setSubmitText("确认");
            tipsDialog.setCancelText("取消");
            tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
                @Override
                public void onClick(int type) {
                    tipsDialog.dismiss();
                    if (type == TipsDialog.TYPE_SUBMIT) {
                        presenter.downTask(managerTask.getTask().getId());
                    }
                }
            });
            tipsDialog.show();
        } else if (msg!=null && msg.equals("addMoney_tv")){
            final InputDialog inputDialog = CommonDialogCollect.addMoney(getContext());
            inputDialog.setOnClickListener(new InputDialog.OnClickListener() {
                @Override
                public void onClick(int button) {
                    if (button == InputDialog.BUTTON_CANCEL) {
                        inputDialog.dismiss();
                    } else if (button == InputDialog.BUTTON_SUBMIT) {
                        EditText editText = inputDialog.getInputView();
                        if (Tool.isEditEmpty(editText)) {
                            ZxToast.showBottomToastLight("请输入金额");
                            return;
                        }
                        BigDecimal money = new BigDecimal(editText.getText().toString());
                        if (money.floatValue() < 0.2f) {
                            ZxToast.showINFOBottomToast("请至少提高0.2元");
                            return;
                        }
                        presenter.addMoney(managerTask.getTask().getId() + "", money);
                        inputDialog.dismiss();
                    }
                }
            });
            inputDialog.show();
        }
    }

    @Override
    public void onTaskDetail(AddTask addTask, ManagerTask managerTask) {
        loadingDialog.cancel();
        Tool.startPublishTaskActivity(getContext(),1,addTask);
    }
}
